<%--
  document: get-orders.jsp
  
  description:
    This page displays submited orders.
 --%>

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@page import="javax.naming.*" %>
<%@page import="javax.transaction.*" %>
<%@page import="com.reimscopie.*" %>
<%@page import="com.reimscopie.beans.interfaces.* "%>

<%-- Tag libs --%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="core"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="html"%>

<%@include file="jspf/head.jspf" %>


<h1>Voir les commandes</h1>

<%
    Context context = new InitialContext() ;
    String name ="com.reimscopie.beans.session.ReimsCopieAdminBean_" +
    ReimsCopieAdminLocal.class.getName()+"@Local" ;    
    ReimsCopieAdminLocal bean = (ReimsCopieAdminLocal)context.lookup(name) ;   
    
    String transactionName = "javax.transaction.UserTransaction" ;
    UserTransaction transaction =
            (UserTransaction)context.lookup(transactionName);
    transaction.begin() ;
    
    if (request.getParameter("done") != null) {
      long id = Long.parseLong(request.getParameter("done")) ;
      bean.unvalidOrder(id) ;
    }
    
    
    Order[] orders ;
    try {
      orders = bean.getValidOrders() ;
      transaction.commit() ;
    }
    catch (Exception e) {
      e.printStackTrace() ;
      transaction.rollback() ;
      return ;
    }
    
    
    for (Order order : orders) {
      Purchase[] purchases = order.getPurchases() ;
      //PersonalInfo info = order.get
      
      out.print("<ul>") ;
      out.print("<li>Commande : ") ;
      out.print(String.valueOf(order.getId())) ;
      out.print(" <a href='get-orders.jsf?done="+order.getId()+"'>") ;
      out.print("Terminé") ;
      out.print("</a>") ;
      out.print("</li>") ;
      out.print("<ul>") ;
      
      // date
      out.print("<li>Date :") ;
      out.print(order.getCommitmentDate()) ;
      out.print("</li>") ;
      
      // cout total
      out.print("<li>Coût total : ") ;
      out.print(String.valueOf(order.getTotalCost())) ;
      out.print("</li>") ;
      
      /*// destinataire
      out.print("<li>Destinataire : ") ;
      out.print(info.getFirstName()) ;
      out.print(' ') ;
      out.print(info.getName()) ;
      out.print(", ") ;
      out.print(info.getStreet()) ;
      out.print(' ') ;
      out.print(info.getPostalCode()) ;
      out.print(' ') ;
      out.print(info.getCity()) ;
      out.print(' ') ;
      out.print(info.getCountry()) ;
      out.print("</li>") ;*/
      
      for (Purchase purchase : purchases) {
        Product product = purchase.getProduct() ;
        
        out.print("<li>Produit : ") ;
        out.print(product.getName()) ;
        out.print("</li>") ;
        out.print("<ul>") ;
        
        out.print("<li>Quantité : ") ;
        out.print(purchase.getQuantity()) ;
        out.print("</li>") ;
        
        out.print("<li>Coût total : ") ;
        out.print(purchase.getTotalCost()) ;
        out.print("</li>") ;
        out.print("</ul>") ;
      }
      
      out.print("</ul>") ;
      out.print("</ul><br>") ;
    }
%>
  
<%@include file="jspf/foot.jspf" %>
